package com.portal.tag;

import javax.portlet.BaseURL;
import javax.portlet.PortletResponse;
import javax.portlet.RenderResponse;
import javax.portlet.ResourceResponse;
import javax.portlet.ResourceURL;
import javax.servlet.jsp.JspException;

import com.portal.util.constants.TagConstants;

public class ResourceURLTag286 extends BaseURLTag {

  private static final long serialVersionUID = 286L;

  private String id = null;

  private String cachability = null;

  private ResourceURL resourceURL = null;

  public ResourceURLTag286() {
    super();
    this.escapeXml = true;
  }

  @Override
  public int doStartTag() throws JspException {
    PortletResponse portletResponse = (PortletResponse) pageContext.getRequest().getAttribute(TagConstants.PORTLET_RESPONSE);
    if (portletResponse != null) {
      ResourceURL resourceURL = createResourceURL(portletResponse);
      if (id != null) {
        resourceURL.setResourceID(id);
      }

      if (cachability != null) {
        try {
          resourceURL.setCacheability(cachability);
        } catch (IllegalArgumentException e) {
          throw new JspException(e);
        } catch (IllegalStateException e) {
          throw new JspException(e);
        }
      }
      setUrl(resourceURL);
    }
    return super.doStartTag();
  }

  public String getId() {
    return id;
  }

  public void setId(String id) {
    this.id = id;
  }

  public String getCachability() {
    return cachability;
  }

  public void setCachability(String cachability) {
    this.cachability = cachability;
  }

  @Override
  protected ResourceURL getUrl() {
    return resourceURL;
  }

  @Override
  protected void setUrl(BaseURL url) {
    this.resourceURL = (ResourceURL) url;
  }

  protected ResourceURL createResourceURL(PortletResponse portletResponse) throws JspException {
    ResourceURL result = null;
    if (portletResponse instanceof RenderResponse) {
      result = ((RenderResponse) portletResponse).createResourceURL();
    } else if (portletResponse instanceof ResourceResponse) {
      result = ((ResourceResponse) portletResponse).createResourceURL();
    } else {
      throw new JspException();
    }
    return result;
  }
}
